package uk.ac.lkl.common.allocator.ui;



import java.awt.Color;
import java.awt.Dimension;

import java.awt.event.*;
import javax.swing.*;


/**
 * Doesn't listen to model since inefficient for every cell to do
 * this. Is just a 'color'.
 * 
 * @author Darren Pearce
 * @version $Revision: 625 $
 * @version $Date: 2008-02-22 14:45:30 +0000 (Fri, 22 Feb 2008) $
 * 
 */
public class CellBlock extends JPanel {

    public static Color NONE = Color.LIGHT_GRAY;

    private int count = 0;

    private JCheckBox checkBox;

    // mobile = moving in some way through interaction with view by
    // controller
    private boolean mobile;

    // hack
    private static Dimension PREFERRED_SIZE = new Dimension(80, 40);


    public CellBlock(boolean useCheckBox) {
        // hack - means that can't ever get the check boxes back after
        // construction
        if (useCheckBox) {
            checkBox = new JCheckBox();
            checkBox.setOpaque(false);
            MouseListener [] mouseListeners = checkBox.getMouseListeners();
            for (MouseListener listener : mouseListeners)
                checkBox.removeMouseListener(listener);
            MouseMotionListener [] mouseMotionListeners = checkBox.getMouseMotionListeners();
            for (MouseMotionListener listener : mouseMotionListeners)
                checkBox.removeMouseMotionListener(listener);
            add(checkBox);
        }
        setOpaque(true);
        setBackground(Color.blue);
        updateColor();
        setBorder(BorderFactory.createEtchedBorder());
        setMobile(false);
    }


    public void setCheckBoxVisible(boolean visible) {
        checkBox.setVisible(visible);
    }


    public Dimension getPreferredSize() {
        return PREFERRED_SIZE;
    }
    
    //public Dimension getMinimumSize() {
    //    return PREFERRED_SIZE;
    //}


    public void setMobile(boolean mobile) {
        if (mobile == this.mobile)
            return;

        this.mobile = mobile;
        updateColor();
    }


    public boolean isMobile() {
        return mobile;
    }


    public void incrementCount() {
        setCount(count + 1);
    }


    public void decrementCount() {
        setCount(count - 1);
    }


    public void setCount(int count) {
        if (count < 0)
            return;

        if (count == this.count)
            return;

        this.count = count;
        updateColor();
    }


    public int getCount() {
        return count;
    }


    private void updateColor() {
        Color color;

        if (checkBox != null)
            checkBox.setSelected(count > 0);
        
        if (count == 0)
            color = Color.LIGHT_GRAY;
        else if (mobile)
            color = Color.YELLOW;
        else {
        	//color = Color.
        	// using green as blue
            int green = 100 - (count - 1) * 34;
            if (green < 0)
                green = 0;
            color = new Color(0, 10, green);
        }
        setBackground(color);
    }

}
